import { childrenProps, BasicForm, FormKeys } from '@/components/ProForm/interface/index';
import { PublicInterface } from '@/interface/public';

import { defaultStyle } from '@/basic/style';
import { DictionariesInterface } from '@/api/interface/basic/dictionaries';

export function useAddForm(proTableBox: PublicInterface.ProTableValue) {
  let addForm = reactive<BasicForm<DictionariesInterface.ReqSave>>({
    form: {
      id: null,
      label: '',
      code: '',
      value: '',
      sort: null,
      pid: null,
      editRoleId: '',
      type: null,
      remark: ''
    },
    hidePropList: ['editRoleId'],
    title: '',
    visible: false,
    options: [
      {
        label: 'id',
        prop: 'id',
        hide: true,
        type: 'input',
        defaultValue: ''
      },

      {
        type: 'input',
        label: '字典名称',
        hide: false,
        prop: 'label',
        defaultValue: ''
        // rules: [{ required: true, message: '字典名称', trigger: 'blur' }]
      },
      {
        type: 'input',
        label: '编号',
        hide: false,
        prop: 'code',
        defaultValue: ''
      },
      {
        type: 'select',
        label: '可见人员',
        hide: false,
        defaultValue: 0,
        prop: 'type',
        children: statusDictVisibleType,
        rules: [{ required: true, message: '可见人员', trigger: 'blur' }],
        changeMethod: form => {
          const flag = form.type === 0;
          if (flag) addForm.form.editRoleId = '';
          formSetAddFormHidePropList();
        }
      },
      {
        type: 'select',
        label: '可编辑角色',
        hide: true,
        defaultValue: [],
        prop: 'editRoleId',
        attrs: {
          multiple: true
        }
      },
      {
        type: 'input',
        label: '字典值',
        hide: false,
        prop: 'value',
        defaultValue: ''
      },

      {
        type: 'input',
        label: '排序',
        hide: false,
        defaultValue: 0,
        prop: 'sort'
      },
      {
        type: 'select',
        label: '父级',
        hide: false,
        defaultValue: '',
        children: [],
        prop: 'pid',
        attrs: {
          options: [],
          clearable: true
        }
      },

      {
        type: 'input',
        label: 'remark',
        defaultValue: '',
        hide: false,
        prop: 'remark',
        attrs: {
          style: defaultStyle
        }
      }
    ]
  });

  const saveAddForm = async form => {
    const params = { ...form };
    params.editRoleId = arrSimpleJoinToStr(form.editRoleId);
    params.pid = params.pid || 0;
    await basicDataDictionarySave(params);
    addForm.visible = false;
    elMessage();
    proTableBox.value?.getTableList();
  };

  /** 设置表单中隐藏属性 */
  const formSetAddFormHidePropList = async () => {
    const isAdmin = useAuthStore().roles.includes(BasicKey.IDENTITY_ADMIN);
    const list: FormKeys<DictionariesInterface.ReqSave>[] = ['editRoleId', 'type'];
    formChangeItemStatus(addForm, list, !isAdmin, 'hide');

    const flag = addForm.form.type === 0;
    formChangeItemStatus(addForm, addForm.hidePropList, flag, 'hide');
  };

  /**
   * 获取表单中的选择数据
   */
  const getAddFormChildren = async () => {
    const res = await promiseAllApi([roleFindList(), basicDataDictionaryFlattenTree()]);

    const children: Map<FormKeys<DictionariesInterface.ReqSave>, childrenProps[]> = new Map();
    children.set('editRoleId', arrTransitionSelectData(res[0], 'name', 'id'));
    children.set('pid', arrTransitionTreeData(res[1], 'label', 'id'));
    formSetChildren(addForm, children);
  };

  return { addForm, saveAddForm, getAddFormChildren, formSetAddFormHidePropList };
}
